import copy
import pygame
from .. import setup, constants, tools
from ..components import info


global_game_info = {
            "score": 0,
            "lives": 1,
        }

class MainMenu:
    def __init__(self):
        game_info = copy.deepcopy(global_game_info)
        self.start(game_info)

    def start(self, game_info):
        self.game_info = game_info
        self.setup_background()
        self.setup_player()
        self.setup_cursor()
        self.info = info.Info("main_menu", self.game_info)
        self.finished = False
        self.next = "load_screen"

    def setup_background(self):
        # 地图背景
        self.background = setup.graphs["level_1"]
        self.background_rect = self.background.get_rect()
        self.background = pygame.transform.scale(self.background,
                                                 (
                                                     int(self.background_rect.width * constants.BG_MULTI),
                                                     int(self.background_rect.height * constants.BG_MULTI)
                                                 )
                                                 )
        self.viewport = setup.SCREEN.get_rect()
        # 标题背景
        self.caption = tools.get_image(setup.graphs["title_screen"], 1, 60, 176, 88, (255, 0, 220), constants.BG_MULTI)

    def setup_player(self):
        self.player_image = tools.get_image(setup.graphs["mario_bros"], 178, 32, 12, 16, (0, 0, 0), constants.PLAYER_MULTI)

    def setup_cursor(self):
        self.cursor = pygame.sprite.Sprite()
        self.cursor.state = "1P"
        self.cursor.image = tools.get_image(setup.graphs["item_objects"], 24, 160, 8, 8, (0, 0, 0), constants.PLAYER_MULTI)
        rect = self.cursor.image.get_rect()
        rect.x, rect.y = (220, 360)
        self.cursor.rect = rect

    def update_cursor(self, keys):
        if keys[pygame.K_UP]:
            self.cursor.state = "1P"
            self.cursor.rect.y = 360
        elif keys[pygame.K_DOWN]:
            self.cursor.state = "2P"
            self.cursor.rect.y = 405
        elif keys[pygame.K_RETURN]:
            self.reset_game_info()
            if self.cursor.state == "1P":
                self.finished = True
            elif self.cursor.state == "2P":
                self.finished = True

    def update(self, surface, keys):

        self.update_cursor(keys)

        surface.blit(self.background, self.viewport)
        surface.blit(self.caption, (170, 100))
        surface.blit(self.player_image, (110, 490))
        surface.blit(self.cursor.image, self.cursor.rect)

        self.info.update()
        self.info.draw(surface)

    def reset_game_info(self):
        self.game_info = copy.deepcopy(global_game_info)
